package com.market.controller.admin;

import com.market.bean.MarketAdmin;
import com.market.bean.bo.admin.AdminCreateBo;
import com.market.bean.bo.admin.AdminDeleteBo;
import com.market.bean.bo.admin.AdminUpdateBo;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminCreateVo;
import com.market.bean.vo.admin.AdminListEntity;
import com.market.bean.vo.admin.AdminUpdateVo;
import com.market.exception.ValidationException;
import com.market.service.admin.AdminAdminService;
import com.market.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统管理模块-管理员管理
 * @since 2022/12/28 20:00
 * @author 2731235517@qq.com 谢畅
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    AdminAdminService adminAdminService;

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo,String username) {
        CommonData<AdminListEntity> commonData = adminAdminService.list(basePageInfo,username);
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Validated AdminCreateBo adminCreateBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        List<MarketAdmin> list = adminAdminService.getAdmins();
        for (MarketAdmin marketAdmin : list) {
            if (marketAdmin.getUsername().equals(adminCreateBo.getUsername())){
                return BaseRespVo.invalidParameter("该管理员已经存在");
            }
        }
        AdminCreateVo adminCreateVo = adminAdminService.create(adminCreateBo);
        return BaseRespVo.ok(adminCreateVo);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Validated AdminUpdateBo adminUpdateBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        List<MarketAdmin> list = adminAdminService.getAdmins();
        for (MarketAdmin marketAdmin : list) {
            if (!marketAdmin.getId().equals(adminUpdateBo.getId()) &&
                    marketAdmin.getUsername().equals(adminUpdateBo.getUsername())){
                return BaseRespVo.invalidParameter("该管理员已经存在");
            }
        }
        AdminUpdateVo adminUpdateVo = adminAdminService.update(adminUpdateBo);
        return BaseRespVo.ok(adminUpdateVo);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody AdminDeleteBo adminDeleteBo) {
        MarketAdmin marketAdmin = adminAdminService.delete(adminDeleteBo);
        return BaseRespVo.ok(marketAdmin);
    }

}
